package com.desksoft.ecommerce.util.html.view.table;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.desksoft.ecommerce.util.html.PageXml;
import com.desksoft.ecommerce.util.html.element.PageEventElement;
import com.desksoft.ecommerce.util.html.element.QueryElement;
import com.desksoft.ecommerce.util.html.element.query.QueryProcedureElement;
import com.desksoft.ecommerce.util.html.element.query.QuerySqlElement;
import com.desksoft.utility.string.StringUtil;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 
 * 创建作者：luoy
 * 文件名称：主表定义
 * 版本：
 * 功能：
 * 最后修改时间：
 * 修改记录：
 *****************************************/
public class MainTable implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5988408026610236344L;
	
	protected String tableId;
	protected List<Column> columnList;
	
	protected PageXml pageXml;
	
	/**
	 * 统计字段查询
	 */
	protected QueryElement statistics;
	/**
	 * 统计字段查询类型
	 */
	protected String statisticsType;
	
	/**
	 * 表格自定义鼠标事件
	 */
	protected List<PageEventElement> eventL;
	
	public MainTable(Element element, PageXml pageXml){
		this.tableId = element.attributeValue("id");
		this.pageXml = pageXml;
		
		Element elmtColumns = element.element("columns");
		this.columnList = parseColumn(elmtColumns);
		Element statisticsElmt = element.element("statistics");
		this.statistics = parseStatistics(statisticsElmt);
		
		Element listenerElmt = element.element("listener");
		if (listenerElmt != null){
			this.eventL = parseListenerElements(listenerElmt.elements("event"));
		}
	}
	
	/**
	 * 解析自定义鼠标事件查询元素
	 */
	public List<PageEventElement> parseListenerElements(List<Element> eventElement){
		if (eventElement == null){
			return null;
		}
		
		List<PageEventElement> eventL = new ArrayList<PageEventElement>();
		String type = "", function = "";
		Element funElmt = null;
		for (Element evtElmt : eventElement){
			eventL.add(new PageEventElement(evtElmt));
		}
		return eventL;
	}
	
	/**
	 * 解析统计查询元素
	 */
	public QueryElement parseStatistics(Element statisticsElmt){
		
		if (statisticsElmt == null){return null;}
		
		Element queryElement = statisticsElmt.element("queryElement");
		
		Element sqlElement = queryElement.element("sqlElement");
		if (sqlElement != null){
			this.statisticsType = "sql";
			return new QuerySqlElement(sqlElement, this.pageXml);
		}
		
		Element procedureElement = queryElement.element("procedureElement");
		if (procedureElement != null){
			this.statisticsType = "procedure";
			return new QueryProcedureElement(procedureElement, this.pageXml);
		}
		
		return null;
	}

	/**
	 * 解析列
	 */
	public List<Column> parseColumn(Element element){
		List<Element> elmtC = element.elements("column");
		List<Column> res = new ArrayList<Column>(elmtC.size());

		String dspy, name;
		int width = 100;
		String hide;
		
		Column cm;
		for (Element elmt : elmtC){
			dspy = elmt.attributeValue("display");
			name = elmt.attributeValue("name");
			String w = elmt.attributeValue("width");
			if (!StringUtil.isEmptyJs(w)){
				width = Integer.parseInt(w);
			}
			hide = "true".equals(elmt.attributeValue("hide")) ? "true" : "false";
			cm = new Column(dspy, name, width, hide);
			String fmttype = elmt.attributeValue("fmttype");
			if (fmttype != null){
				cm.setFmttype(fmttype);
			}
			res.add(cm);
		}
		return res;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public List<Column> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<Column> columnList) {
		this.columnList = columnList;
	}

	public QueryElement getStatistics() {
		return statistics;
	}

	public void setStatistics(QueryElement statistics) {
		this.statistics = statistics;
	}

	public String getStatisticsType() {
		return statisticsType;
	}

	public void setStatisticsType(String statisticsType) {
		this.statisticsType = statisticsType;
	}

	public List<PageEventElement> getEventL() {
		return eventL;
	}

	public void setEventL(List<PageEventElement> eventL) {
		this.eventL = eventL;
	}
}
